package com.beichuan_travel.utils;

import org.springframework.data.redis.core.*;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.Collection;
import java.util.concurrent.TimeUnit;

@Component
public class RedisUtils {

    @Resource
    RedisTemplate<String, Object> redisTemplate;

    public ValueOperations<String, Object> valueOperations(RedisTemplate<String, Object> redisTemplate) {
        return redisTemplate.opsForValue();
    }

    public ListOperations<String, Object> listOperations(RedisTemplate<String, Object> redisTemplate) {
        return redisTemplate.opsForList();
    }

    public SetOperations<String, Object>  setOperations(RedisTemplate<String, Object> redisTemplate) {
        return redisTemplate.opsForSet();
    }

    public ZSetOperations<String, Object> zSetOperations(RedisTemplate<String, Object> redisTemplate) {
        return redisTemplate.opsForZSet();
    }

    public HashOperations<String, String, Object> hashOperations(RedisTemplate<String, Object> redisTemplate) {
        return redisTemplate.opsForHash();
    }

    /**
     * @Description:
     * @Param: [key, object, timeout, unit]   key: 键   object： 值   timeout：过期时间   unit：时间单位
     * @return: void
     * @Author: Sora
     * @Date: 2021/5/29
     */
    public void setKey(final String key, final Object object, final long timeout, final TimeUnit unit) {
        valueOperations(redisTemplate).set(key, object, timeout, unit);
    }

    public void setKey(final String key, final Object object) {
        setKey(key, object, 3, TimeUnit.DAYS);
    }

    public void setKey(final String key, final Object object, final long timeout) {
        setKey(key, object, timeout, TimeUnit.DAYS);
    }

    public Object getKey(final String key) {
        return getKey(key, 0 , -1);
    }

    public Object getKey(final String key, long start, long end) {
        return redisTemplate.opsForValue().get(key, start, end);
    }

    public Boolean expire(final String key, final Long timeout, final TimeUnit unit) {
        Boolean flag = redisTemplate.expire(key, timeout, unit);
        return flag!=null && flag;
    }

    public Boolean expire(final String key, final Long timeout) {
        Boolean flag = redisTemplate.expire(key, timeout, TimeUnit.DAYS);
        return flag!=null && flag;
    }

    public Boolean deleteKey(final String key) {
        Boolean flag = redisTemplate.delete(key);
        return flag!=null && flag;
    }

    public Long deleteKey(final Collection<String> keys) {
        Long i = redisTemplate.delete(keys);
        return i == null ? 0 : i;
    }

    public Long setSet(final String key, final Object... values) {
        Long i = redisTemplate.opsForSet().remove(key, values);
        return i == null ? 0 : i;
    }

    public Long deleteSet(final String key, Object... values) {
        Long i = redisTemplate.opsForSet().add(key, values);
        return i == null ? 0 :i;
    }

    public Long lPush(final String key, final Object object) {
        Long i = redisTemplate.opsForList().leftPush(key, object);
        return i == null ? 0 : i;
    }

    public Long lPushAll(final String key, Object... objects) {
        Long i = redisTemplate.opsForList().leftPushAll(key, objects);
        return i == null ? 0 : i;
    }

    public Long lPushAll(final String key, Collection<Object> objects) {
        Long i = redisTemplate.opsForList().leftPushAll(key, objects);
        return i == null ? 0 : i;
    }

    public Long rPush(final String key, final Object object) {
        Long i = redisTemplate.opsForList().rightPush(key, object);
        return i == null ? 0 : i;
    }

    public Long rPushAll(final String key, final Object... objects) {
        Long i = redisTemplate.opsForList().rightPushAll(key, objects);
        return i == null ? 0 : i;
    }

    public Long rPushAll(final String key, final Collection<Object> objects) {
        Long i = redisTemplate.opsForList().rightPushAll(key, objects);
        return i == null ? 0 : i;
    }


}
